/*
 * Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved.
 *
 * https://www.mall4j.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */
package com.yami.shop.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.bean.model.Question;
import com.yami.shop.common.annotation.SysLog;
import com.yami.shop.service.QuestionService;
import lombok.AllArgsConstructor;
import com.yami.shop.common.response.ServerResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.Date;

/**
 * 用户问题管理
 *
 * @author hzm
 * @date 2019-04-15 10:49:40
 */
@RestController
@AllArgsConstructor
@RequestMapping("/shop/question")
public class QuestionController {

    private final QuestionService questionService;

    /**
     * 分页查询
     *
     * @param page     分页对象
     * @param question 用户问题管理
     * @return 分页数据
     */
    // @GetMapping("/page")
    // public ServerResponseEntity<IPage<Question>> getQuestionPage(PageParam page, Question question) {
    //     return ServerResponseEntity.success(questionService.page(page, new LambdaQueryWrapper<Question>()));
    // }
    @GetMapping("/page")
    public ServerResponseEntity<IPage<Question>> getQuestionPage(PageParam<Question> page, Question question) {
        // 使用 MyBatis-Plus 的 Page 类
        // IPage<Question> pageQuery = new Page<>(Question.getCurrent(), Question.getSize());
        
        IPage<Question> questionPage = questionService.page(
            page,
            new LambdaQueryWrapper<Question>()
                .eq(question.getStatus() != null, Question::getStatus, question.getStatus())
                .nested(question.getTitle() != null, 
                    wrapper -> wrapper
                        .like(Question::getTitle, question.getTitle())
                        .or()
                        .like(Question::getQuestion, question.getTitle())
                )
                .orderByDesc(Question::getUpdateTime)
        );
        
        return ServerResponseEntity.success(questionPage);
    }


    /**
     * 通过id查询用户问题管理
     *
     * @param questionId id
     * @return 单个数据
     */
    @GetMapping("/info/{questionId}")
    public ServerResponseEntity<Question> getById(@PathVariable("questionId") Long questionId) {
        return ServerResponseEntity.success(questionService.getById(questionId));
    }

    /**
     * 新增用户问题管理
     *
     * @param question 用户问题管理
     * @return 是否新增成功
     */
    @SysLog("新增用户问题管理")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('shop:question:save')")
    public ServerResponseEntity<Boolean> save(@RequestBody @Valid Question question) {
        // question.setUpdateTime(new Date()); 
        question.setCreateTime(new Date());
        return ServerResponseEntity.success(questionService.save(question));
    }

    /**
     * 修改用户问题管理
     *
     * @param question 用户问题管理
     * @return 是否修改成功
     */
    @SysLog("修改用户问题管理")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('shop:question:update')")
    public ServerResponseEntity<Boolean> updateById(@RequestBody @Valid Question question) {
        return ServerResponseEntity.success(questionService.updateById(question));
    }

    /**
     * 通过id删除用户问题管理
     *
     * @param questionId id
     * @return 是否删除成功
     */
    @SysLog("删除用户问题管理")
    @DeleteMapping("/{questionId}")
    @PreAuthorize("@pms.hasPermission('shop:question:delete')")
    public ServerResponseEntity<Boolean> removeById(@PathVariable Long questionId) {
        return ServerResponseEntity.success(questionService.removeById(questionId));
    }

}